package com.bcxj.servicemap.remote;

import com.bcxj.internalcommon.constant.AmapConfigConstants;
import com.bcxj.internalcommon.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * ClassName: ServiceClient
 * Package: com.bcxj.servicemap.mapper
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/10/31 21:31
 * @Version 1.0
 * @Since 1.0
 */
@Service
@Slf4j
public class ServiceClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 添加服务
     * @param name
     * @return
     */
    public ResponseResult addTerminal(String name) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.SERVICE_ADD_URL);
        url.append("?key=").append(amapKey);
        url.append("&name=").append(name);

        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url.toString(), null, String.class);

        String body = stringResponseEntity.getBody();

//        System.out.println(url);
//        System.out.println(body);

        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        String id = data.getString("sid");
        log.info("添加服务成功，sid:{}", id);

        return ResponseResult.success();
    }

}
